import moment from 'moment'
import publicFunc from "@/util/public"

function plugin(Vue) {
    if (Vue && Vue.filter) {

        // filter to limit content to 100 strs
        Vue.filter('cut', value => value.length >= 100 ? value.substring(0, 100) + '...' : value)

        // format date from iso8601 string
        Vue.filter('formatDateByIso', value => {
            return value && moment(value).format('YYYY-MM-DD HH:mm:ss')
        })

        Vue.filter('addtime', (value, type) => {
            return value && value + (!type ? ' 00:00:00' : ' 24:00:00')
        })

        Vue.filter('numBlank', (value) => {
            if (value==undefined){
                return "--";
            }
            return value;
        })

        Vue.filter('dateformat', function(v) {
            if (v) {
                return moment(v).format('YYYY-MM-DD')
            }
            return '';
        });

        Vue.filter('formatDate', function(v) {
            if (v) {
                return moment(v).format('HH:mm')
            }
            return '';
        });

        // format price 22 => 22.00
        Vue.filter('priceformat', value => Number(value).toFixed(2))
        Vue.filter('toInt', value => Number(value).toFixed(0))

        Vue.filter("url",function(value){
            if (arguments.length>1){
                var reg = /\/$/;
                if(reg.test(value)==false){
                    value = value+"/";
                }
                var url = value;
                for(var i= 1;i<arguments.length;i++){
                    url += arguments[i]+"/";
                }
                return url;
            }
            return  value;
        });

        Vue.filter("truncatechars",function(value){
            return value;
        });

        Vue.filter("date",function(value,format_s){
            if (format_s=="H:i Y-m-d"){
                return value && moment(value).format('HH:mm YYYY-MM-DD')
            }else if(format_s){
                moment.locale('zh-cn');
                return value && moment(value).format(format_s);
            }
            return value;

        });
        Vue.filter("time",function(value,format_s){
            if (format_s=="H:i"){
                return value && moment(value).format('HH:mm')
            }else if(format_s){
                return value && moment(value).format(format_s);
            }
            return value;

        });
        Vue.filter('headerImg',function (value) {
            if (!value || value==='/0') {
                return 'http://sy.ykt.io/defaultuser.png'
            }
            return value
        })
        Vue.filter("imgBlank", function (v) {
            if (!v) {
                return undefined
            }
            return v
        })
        Vue.filter("titleTime", function (v) {
            if (!v) {
                return ""
            }
            v = v.replace(/\-/g,'/')
            let date = new Date(v)
            return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日"
        });
        Vue.filter("toFixed", function (value,n) {
            return publicFunc.toFixed(value, n)
        });
    }
}

if (typeof define === 'function' && define.amd){
    // AMD. Register as an anonymous module.
    define(['exports'], function(){
        return plugin;
    });
}

export default plugin
